﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Solution.Business.Component;
using Solution.Business.Entities;
using Solution.Data;
using System.IO;
using System.Drawing;
using Solution.UI.Web.Models;
using System.Data;

namespace Solution.UI.Web.Controllers.Admin
{
    public class AdminAdvertiseController : Controller
    {
        private SolutionEntities db = new SolutionEntities();
        //
        // GET: /AdminAdvertise/

        public ActionResult Index()
        {
            var list = db.Advertises.OrderByDescending(c => c.AId);
            return View(list.ToList());
        }

        public ActionResult Details(int id = 0)
        {
            Advertise advertise = db.Advertises.Find(id);
            if (advertise == null)
            {
                return HttpNotFound();
            }
            return View(advertise);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Advertise advertise, int ViTri, HttpPostedFileBase hinhanh)
        {
            if (ModelState.IsValid)
            {
                if (ViTri == 1)
                {
                    advertise.Position = 1;
                }
                else if (ViTri == 2)
                {
                    advertise.Position = 2;
                }

                if (advertise.Name != null && hinhanh != null && advertise.Position != null)
                {
                    db.Advertises.Add(advertise);
                    db.SaveChanges();

                }
                else
                {
                    ModelState.AddModelError("", "Bạn chưa nhập đầy đủ thông tin vào các ô có đánh dấu * !");
                }
                    var temp = (from element in db.Advertises
                                where element.AId.Equals(advertise.AId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image = "/images/" + temp.AId + "_" + hinhanh.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath = Path.Combine(Server.MapPath("~/images/"), temp.AId + "_" + hinhanh.FileName);
                        var images = Image.FromStream(hinhanh.InputStream);
                        var newImage = ImageScale.ScaleImage(images, 372, 200);
                        newImage.Save(filePath);
                    }
                    db.SaveChanges();
                    return RedirectToAction("Index", "AdminAdvertise");
            }
            return View(advertise);
        }

        public ActionResult Edit(int id = 0)
        {
            Advertise advertise = db.Advertises.Find(id);
            if (advertise == null)
            {
                return HttpNotFound();
            }
            return View(advertise);
        }

        [HttpPost]
        public ActionResult Edit(Advertise advertise, int ViTri, HttpPostedFileBase hinhanh)
        {
            if (ModelState.IsValid)
            {
                if (hinhanh != null && hinhanh.ContentLength > 0)
                {
                    if (advertise.Image != null)
                    {
                        var oldFilePath = Server.MapPath("~") + advertise.Image.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath))
                        {
                            System.IO.File.Delete(oldFilePath);
                        }
                    }
                    advertise.Image = "/images/" + advertise.AId + "_" + hinhanh.FileName;
                    var filePath = Path.Combine(Server.MapPath("~/images/"), advertise.AId + "_" + hinhanh.FileName);
                    var image = Image.FromStream(hinhanh.InputStream);
                    var newImage = ImageScale.ScaleImage(image, 372, 200);
                    newImage.Save(filePath);
                }
                    if (ViTri == 1)
                    {
                        advertise.Position = 1;
                    }
                    else if (ViTri == 2)
                    {
                        advertise.Position = 2;
                    }

                    db.Entry(advertise).State = EntityState.Modified;
                    if (advertise.Name != null && hinhanh != null && advertise.Position != null)
                    {
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Bạn chưa nhập đầy đủ thông tin vào các ô có đánh dấu * !");
                    }
            }

            return View(advertise);
        }

        public ActionResult Delete(int id = 0)
        {
            Advertise advertise = db.Advertises.Find(id);
            if (advertise == null)
            {
                return HttpNotFound();
            }
            return View(advertise);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var advertise = db.Advertises.Find(id);
            if (advertise.Image != null)
            {
                var FilePath = Server.MapPath("~") + advertise.Image.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(FilePath))
                {
                    System.IO.File.Delete(FilePath);
                }
            }

            db.Advertises.Remove(advertise);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
